<?php
namespace app\admin\controller;
use think\Db;
/**
 * 商家
 * Class Member
 */
class NewsController extends BaseController {

	public function index(){
		$list=db('news')->alias('a')->join('newstype b','a.cart_id=b.id')->field('a.id,b.name,a.title,a.add_time,a.clock')->paginate(5);
		$this->assign('list',$list);
		 return  $this->fetch();
	}
	public function update($id){
		if ($this->request->isPost()) {
			$data=get_query()['query'];
		    $data['editorValue']=serialize($data);
	
			$res=db('news')->update($data);
			if($res){
				$this->success('更新成功',url("index"));
				
			}else{
				$this->success('更新失败');	
			}
			
		}
		$news=db('news')->alias('a')->join('newstype b','a.cart_id=b.id')->where('a.id',$id)->select();
		$result = Db('newstype')->where('parent_id',0)->select();
		$this->assign('result',$result);
        $this->assign('news',$news);
        return $this->fetch();
		
	}
	public function add(){
		if ($this->request->isPost()) {
			$data=get_query()['query'];;
			$data['add_time']=now_time();
			$res=db('news')->insert($data);
			if($res){
				$this->success('添加成功',url("index"));
				
			}else{
				$this->success('添加失败');	
			}
			
		}
		$result = Db('newstype')->where('parent_id',0)->select();
		$this->assign('result',$result);
		return $this->fetch();
	}
	
	public function news_list(){
		$cart_id = input('cart_id');
		$result = Db('newstype')->where('parent_id',$cart_id)->select();
		exit(json_encode($result));
	}
}
